// LineList.cpp: implementation of the CLineList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LineList.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLineList::CLineList()
{

	m_pFirstItem = NULL;
}

CLineList::~CLineList()
{

	// Delete all items
	CLineItem *pLine, *pLineTmp;

	pLine = m_pFirstItem;

	while(pLine) {

		pLineTmp = pLine->m_pNext;

		delete pLine;
		pLine=NULL;
		pLine = pLineTmp;

	}
		
}

void CLineList::Add(CLineItem *pLine)
{

	// Is this the first item?
	if (m_pFirstItem == NULL) {

		m_pFirstItem = pLine;

		return;

	}

	// Walk the list until we find the end
	CLineItem* pTemp = m_pFirstItem;

	while(pTemp->m_pNext != NULL) {

		pTemp = pTemp->m_pNext;

	}

	// Ok, we found the end of the list
	pTemp->m_pNext = pLine;

}

CLineItem* CLineList::GetFirst()
{

	return m_pFirstItem;
}

void CLineList::RemoveAll(void)
{
	CLineItem* pLine;
	CLineItem* pTemp;

	pLine = m_pFirstItem;

	while(pLine) {

		pTemp = pLine->m_pNext;

		delete pLine;
		pLine=NULL;
		pLine = pTemp;

	}

	m_pFirstItem = NULL;

}


void CLineList::RemoveFirst()
{
	if(m_pFirstItem==0){
		return;
	}
	if(m_pFirstItem->m_pNext==0){
		delete m_pFirstItem;
		m_pFirstItem=NULL;
	}
	else{
		CLineItem * tmpLine=m_pFirstItem->m_pNext;
		delete m_pFirstItem;
		m_pFirstItem=NULL;
		m_pFirstItem = tmpLine;
	}
}
